#coding:utf-8

# --------------------------------------------------------------------------------
# 上岗持证要求的管理
# --------------------------------------------------------------------------------
module OrganRegisterService
  def self.registered(app)

    app.get "/?" do
      OrganRegisterDomain.all.to_json
    end
    
    app.get "/:id" do
      param :id, Integer, required: true
      
      elem =  OrganRegisterRepo[params[:id]]
      elem.nil? ? raise("not found organ register: #{params[:id]}") : elem.perfect.to_json
    end

    app.post "/?" do
      param :organ_id,    String,   required: true
      param :manager_id,  Integer,  required: true

      OrganRegisterDomain.create(params.symbolize_keys).to_json
    end


    app.delete "/:id/?" do
      param :id, Integer, required: true
      
      elem = OrganRegisterRepo[params[:id]]
      if elem
        begin
          elem.delete
          {result: :ok, id: elem.id}.to_json
        rescue SQLite3::ConstraintException
          {result: :error, message: "delete organ register failed, id: #{elem.id}"}.to_json
        end
      else
        {result: :error, message: "delete organ register failed, not found id: #{params[:id]}"}.to_json
      end
    end
  end
end
